const fs = require("fs-extra")
const execa = require("execa")
const { prompt } = require("enquirer")

const run = (bin, args, opts = {}) => execa(bin, args, { stdio: "inherit", ...opts })

async function getCommitMessage() {
  const { msg } = await prompt({
    type: "input",
    name: "msg",
    message: "Input Message For Current Commit",
  })
  return msg
}

async function buildProject() {
  try {
    await run("npm", ["run", "build"])
  } catch (error) {
    throw error
  }
}

async function commitChanges() {
  const { stdout } = await run("git", ["diff"], { stdio: "pipe" })
  if (stdout) {
    await run("git", ["add", "-A"])
    const msg = await getCommitMessage()
    await run("git", ["commit", "-m", msg])
  } else {
    console.log("No changes to commit.")
  }
}

async function publishToGitee() {
  await run("git", ["push", "origin", "master"])
}

async function publishToGithub() {
  await run("git", ["push", "-f", "github", "master"])
}

async function main() {
  await buildProject()
  await commitChanges()
  await publishToGitee()
  await publishToGithub()
}

main()
